package gui;

import filesystem.FileSystemModel;
import gui.charts.PieChart3D;
import gui.listener.MyContentListener;
import gui.settings.ConfigFile;
import gui.settings.ProgressBar;
import gui.settings.SettingsPanel;
import gui.tree.FileSystemTreePanel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.io.File;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

import lbd.ConsolePanel;
import lbd.Lbd;
import lbd.TermFreq;

public class MainFrame extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private JScrollPane jScrollPaneTree;
	private JScrollPane jScrollPane1stChart;
	private JScrollPane jScrollPane2ndChart;
	private JScrollPane jScrollPaneSettings;
	
	private JPanel 		jPanelConsole;	
	
	public SettingsPanel settingsPanel;	
	
	public	String		path1st		= "";
	public	String		extension1st	= "";
	public  LinkedList<TermFreq> termList1st;
	
	public	String		path2nd		= "";
	public	String		extension2nd	= "";
	public  LinkedList<TermFreq> termList2nd;
	
	private static final Insets insets = new Insets(0, 0, 0, 0);

	public MainFrame(FileSystemTreePanel fileTreePanel, PieChart3D pieChart3D) {	
		super();
		initGUI(fileTreePanel, pieChart3D);		
	}
	
	private void initGUI(FileSystemTreePanel fileTreePanel, PieChart3D pieChart3D) {
		
		try {
		    
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(new java.awt.GridBagLayout());
		    
			setTitle("LoOk BeHiNd Data");
		    
		    jScrollPaneTree = new JScrollPane(fileTreePanel);			    		    
		    addComponent(jScrollPaneTree, 0, 0, 1, 3, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 0.5, 1.0);
		    
		    jScrollPane1stChart = new JScrollPane();
			addComponent(jScrollPane1stChart, 1, 0, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 1.0, 1.0);
	
			jScrollPane2ndChart = new JScrollPane();
			addComponent(jScrollPane2ndChart, 1, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 1.0, 1.0);
			
			settingsPanel = new SettingsPanel(this);
			jScrollPaneSettings = new JScrollPane(settingsPanel);
			addComponent(jScrollPaneSettings, 3, 0, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 0.42, 0);
			settingsPanel.setNotEnabled();
			
	        jPanelConsole = new ConsolePanel();
	        addComponent(jPanelConsole, 1, 2, 3, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 1.0 , 0.3);
			
	        this.setSize(new Dimension(1024,768));
			this.setLocationRelativeTo(null);
		    setVisible(true);
		    
		} catch (Exception e) {
			
			e.printStackTrace();
			
		}
		
	}
	
	private void addComponent(Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill, double weightx, double weighty) {
		    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, weightx, weighty, anchor, fill, insets, 0, 0);
		    getContentPane().add(component, gbc);
	}
	
	public void updateChart(){
		
		if(!path1st.equals("")){
			PieChart3D pieChart3D = 
				new PieChart3D	(	path1st, 
									Lbd.filterTermsList(	termList1st, 
															ConfigFile.getInstance().getTERM_NUM(), 
															ConfigFile.getInstance().getTERM_MIN_FREQ()
														)
								);
			jScrollPane1stChart.setViewportView(pieChart3D.createPanel());
		}
		
		if(!path2nd.equals("")){
			PieChart3D pieChart3D = 
				new PieChart3D	(	path2nd, 
									Lbd.filterTermsList(	termList2nd, 
															ConfigFile.getInstance().getTERM_NUM(), 
															ConfigFile.getInstance().getTERM_MIN_FREQ()
														)
								);
			jScrollPane2ndChart.setViewportView(pieChart3D.createPanel());
		} else {
			jScrollPane2ndChart.setViewportView(new PieChart3D().createPanel());
		}
		
	}
	
	public void updateIndex(){
		
		//resetTextArea();
		
		if(!path1st.equals(""))
			termList1st = Lbd.getTermList(	new File(path1st), extension1st, 20, 0, ((ConsolePanel)jPanelConsole).jTextAreaConsole );
		if(!path2nd.equals(""))
			termList2nd = Lbd.getTermList(	new File(path2nd), extension2nd, 20, 0, ((ConsolePanel)jPanelConsole).jTextAreaConsole );
		
	}
	
	public void searchAndDownload(){
		
		final MainFrame mf = this;
		
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ProgressBar.createAndShowGUI(mf);
            }
        });
		
	}
	
	public void writeTextArea(String msg) {
		((ConsolePanel)jPanelConsole).jTextAreaConsole.setText(((ConsolePanel)jPanelConsole).jTextAreaConsole.getText() + msg + "\n");
	}
	
	public void resetTextArea() {
		((ConsolePanel)jPanelConsole).jTextAreaConsole.setText("");
	}
	
	public void empty2ndChart(){
		jScrollPane2ndChart.setViewportView(new PieChart3D().createPanel());
	}
	
	public static void main(String[] args) {
		
		FileSystemModel 		fileSystemDataModel = new FileSystemModel();   
		FileSystemTreePanel 	fileTreePanel 		= new FileSystemTreePanel( fileSystemDataModel );	    
		PieChart3D 				pieChart3D 			= new PieChart3D();	
	    MainFrame 				mainFrame 			= new MainFrame(fileTreePanel, pieChart3D); 		 	
		MyContentListener 		listener			= new MyContentListener(mainFrame);

		fileTreePanel.getTree().addTreeSelectionListener(listener);
		
	}
	
}
